package org.dbstar.scheduler.beans;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.dbstar.scheduler.control.JobController;
import org.dbstar.scheduler.control.exception.DaoException;

import net.sf.json.JSONObject;


/**
 * 基于命令行类型的Job的执行结果
 * 
 * @author 代波
 * @since 1.0.0
 * @version 1.0.0
 * @date 2012-3-26
 */
public abstract class CmdJobResult extends JobResult {
	private static final long serialVersionUID = 2472509366164508484L;

	private int exitValue = -1;

	/**
	 * Returns the exit value for the job process.
	 * 
	 * @return the exit value of the job process represented by this job. by convention, the value <code>0</code>
	 *         indicates normal termination.
	 * @exception IllegalThreadStateException if the job process represented by this job has not yet terminated.
	 */
	public final int getExitValue() {
		return exitValue;
	}

	public final void setExitValue(int exitValue) {
		this.exitValue = exitValue;
	}

	@Override
	public final boolean isSucceed() {
		return exitValue == 0;
	}

	@Override
	public void reExecute(Job job, boolean failedOnly, JobController jobController) throws DaoException {
		super.reExecute(job, failedOnly, jobController);
		setExitValue(-1);
	}

	@Override
	protected void write(JSONObject object) {
		super.write(object);
		object.element("exitValue", exitValue);
	}

	@Override
	protected void read(JSONObject object) {
		super.read(object);
		exitValue = object.optInt("exitValue");
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = super.hashCode();
		result = prime * result + exitValue;
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (!super.equals(obj))
			return false;
		if (getClass() != obj.getClass())
			return false;
		CmdJobResult other = (CmdJobResult) obj;
		if (exitValue != other.exitValue)
			return false;
		return true;
	}

	/**
	 * Gets the input stream of the job process. The stream obtains data piped from the standard output stream of the
	 * process represented by this job.
	 * 
	 * @return the input stream connected to the normal output of the job process.
	 * @throws IOException
	 */
	public abstract InputStream getStandardStream() throws IOException;

	/**
	 * Gets the error stream of the job process. The stream obtains data piped from the error output stream of the
	 * process represented by this job.
	 * 
	 * @return the input stream connected to the error stream of the job process.
	 * @throws IOException
	 */
	public abstract InputStream getErrorStream() throws IOException;

	/**
	 * 获得job执行结果中的标准输出流。
	 * 
	 * @return job执行结果中的标准输出流。
	 * @throws IOException
	 */
	public abstract OutputStream getStdOutStream() throws IOException;

	/**
	 * 获得job执行结果中的错误输出流。
	 * 
	 * @return job执行结果中的错误输出流。
	 * @throws IOException
	 */
	public abstract OutputStream getStdErrStream() throws IOException;
}
